import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { _HttpClient } from '@delon/theme';
import { DoorEditComponent } from '../door-edit/door-edit.component';

@Component({
  selector: 'app-door-list',
  templateUrl: './door-list.component.html',
  styles: []
})
export class DoorListComponent implements OnInit {
  @ViewChild('st',{static:false}) st:TablePageComponent;
  selectedData:any[];
  config: SPage = {
    columns: [
      {
        index: 'sipId',
        title: 'sip',
      },
      {
        index: 'doorDescribe',
        title: '名称',
        filter:{
          placeholder: '请输入名称',
          type: 'string'
        }
      },
      {
        index: 'communityName',
        title: '小区',
      },
      {
        index: 'houseName',
        title: '楼宇'
      },
      {
        index: 'areaName',
        title: '单元'
      },
      {
        index: 'doorType',
        title: '门禁类型',
        type:ColumnType.tag,
        tag:{
          1:{text:'单元门禁'},
          2:{text:'小区门禁'},
        },
      }
    ],
    showCheckBox: true,
    reqUrl: this.api.area.doorPage.url,
    editBtn: [
      // { title: '编辑'},
      {title:'删除'}
    ]
  }

  constructor(private api : ApiService, private msg : NzMessageService, private modal : NzModalService,private http : _HttpClient) { }
  ngOnInit() {
  }
  operate(e: any): void {
    if (e.title === '编辑') {
      this.editDic(1, e.data);
    }else if(e.title == '删除'){
      this.delData(e.data.id);
    }
  }

    // 编辑功能开始
    editDic(type: 0 | 1 = 0, _param?: object): void {
      this.modal.create({
        nzTitle: type === 0 ? '新增'  : '编辑',
        nzContent: DoorEditComponent,
        nzWidth: 800,
        nzComponentParams: {
          type,
          _param
        },
        nzOnOk:()=>{
          this.st.reload();
        },
        nzFooter: null
      })
    }

    delData(id:string){
      this.modal.confirm({
        nzTitle: '确定删除?',
        nzOnOk: () => {
          const body = {};
          this.http.post(this.api.area.doorDel.url+'/'+id,body).subscribe(res => {
            if (res.status === '200') {
              this.msg.success('删除成功');
              this.st.reload();
            }else{
              this.msg.error(res.message);
            }
          })
        }
      })
    }

    _console(e: any): void {
      this.selectedData = e;
    }
}
